package com.edu.whty.dao;

import com.edu.whty.entity.base.SpecialRepo;
import com.edu.whty.entity.school.SpecialResourcesBase;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * TODO
 *
 * @author luyao
 * @explain
 * @date 2021/5/28
 */
public interface SpecialRepoDao {

    @Select("select * from t_special_repo where id=#{id}")
    SpecialRepo selectSpecialRepo(@Param("id") String id);

    @Select({
            "<script>",
            "select * from t_special_repo p",
            "<if test='parentId != null'>where p.parent_id=#{parentId} </if>",
            "<if test='parentId == null'>where (p.parent_id != 'ZT' and p.status='0' and  EXISTS(SELECT  * FROM t_special_repo WHERE id=p.repo_id and parent_id ='ZT' and is_show=1)) or (parent_id ='ZT' and is_show=1 and status='0') </if>",
            " order by p.create_time asc",
            "</script>"
    })
    List< SpecialRepo> selectSpecialRepoList(@Param("parentId") String parentId);

    @Update("update t_special_repo set id=#{id} , name=#{name}, parent_id=#{parentId},res_num=#{resNum} where repo_id=#{repoId} ")
    int  updateSpecialRepo(SpecialRepo specialRepo);

    @Insert("insert into t_special_repo ( id, parent_id, name, res_num, repo_id) values(#{id},#{parent_id},#{name},#{res_num},#{repo_id} )")
    int addSpecialRepor(SpecialRepo specialRepo);

    @Insert({
            "<script>",
            "insert into t_special_repo(id, parent_id, name, res_num, repo_id, is_show, status, create_time) values ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id}, #{item.parentId}, #{item.name},#{item.resNum},#{item.repoId},#{item.isShow},#{item.status},#{item.createTime})",
            "</foreach>",
            "</script>"
    })
    int batchAddSpecialRepo(@Param(value="list")List<SpecialRepo> list);


    @Insert({
            "<script>",
            "		update t_special_repo",
            "		<trim prefix='set' suffixOverrides=','>",
            "			<trim prefix='is_show =case' suffix='end,'>",
            "				<foreach collection='list' item='item' index='index'>",
            "					when id=#{item.id} then #{item.isShow}",
            "				</foreach>",
            "			</trim>",
            "			<trim prefix='status =case' suffix='end,'>",
            "				<foreach collection='list' item='item' index='index'>",
            "					when id=#{item.id} then #{item.status}",
            "				</foreach>",
            "			</trim>",
            "			<trim prefix='parent_id =case' suffix='end,'>",
            "				<foreach collection='list' item='item' index='index'>",
            "					when id=#{item.id} then #{item.parentId}",
            "				</foreach>",
            "			</trim>",
            "			<trim prefix='res_num =case' suffix='end,'>",
            "				<foreach collection='list' item='item' index='index'>",
            "					when id=#{item.id} then #{item.resNum}",
            "				</foreach>",
            "			</trim>",
            "			<trim prefix='repo_id =case' suffix='end,'>",
            "				<foreach collection='list' item='item' index='index'>",
            "					when id=#{item.id} then #{item.repoId}",
            "				</foreach>",
            "			</trim>",
            "			<trim prefix='name =case' suffix='end,'>",
            "				<foreach collection='list' item='item' index='index'>",
            "					when id=#{item.id} then #{item.name}",
            "				</foreach>",
            "			</trim>",
            "		</trim>",
            "		where id in",
            "		<foreach collection='list' index='index' item='item' separator=',' open='(' close=')'>",
            "			#{item.id}",
            "		</foreach>",
            "</script>"
    })
    void batchUpdateSpecialRepo(@Param(value="list")List<SpecialRepo> list);

    @Delete({
            "<script>",
            "		delete from t_special_repo",
            "		where repo_id in",
            "		<foreach collection='list' index='index' item='item' separator=',' open='(' close=')'>",
            "			#{item.id}",
            "		</foreach>",
            "</script>"
    })
    void deleteUserSpecialRepo(@Param(value="list")List<SpecialResourcesBase> list);

    @Delete({
            "<script>",
            "		delete from t_special_repo",
            "		where id in",
            "		<foreach collection='list' index='index' item='item' separator=',' open='(' close=')'>",
            "			#{item.id}",
            "		</foreach>",
            "</script>"
    })
    void deleteSpecialRepo(@Param(value="list")List<SpecialRepo> list);
}
